<template>
  <div class="simple-debug">
    <n-card title="简单调试信息" class="debug-card">
      <template #header-extra>
        <n-button @click="refreshDebugInfo" type="primary" size="small">
          <template #icon>
            <n-icon><RefreshOutline /></n-icon>
          </template>
          刷新
        </n-button>
      </template>
      
      <n-space vertical size="large">
        <!-- 基本信息 -->
        <n-card title="基本信息" size="small">
          <n-space vertical>
            <n-text>工作区数量: {{ allWorkspaces.length }}</n-text>
            <n-text>当前工作区: {{ currentWorkspace?.name || '无' }}</n-text>
            <n-text>当前工作区ID: {{ currentWorkspace?.id || '无' }}</n-text>
          </n-space>
        </n-card>

        <!-- 当前工作区详情 -->
        <n-card title="当前工作区详情" size="small">
          <n-space vertical>
            <div v-if="currentWorkspace">
              <n-text>工作区名称: {{ currentWorkspace.name }}</n-text>
              <br>
              <n-text>文件路径: {{ currentWorkspace.filePath || '未设置' }}</n-text>
              <br>
              <n-text>解压数据大小: {{ currentWorkspace.extractedData ? `${currentWorkspace.extractedData.length} 字符` : '无' }}</n-text>
              <br>
              <n-text>创建时间: {{ currentWorkspace.createTime }}</n-text>
            </div>
            <div v-else>
              <n-text>没有选择工作区</n-text>
            </div>
          </n-space>
        </n-card>

                 <!-- 解压数据预览 -->
         <n-card title="解压数据预览" size="small">
           <n-space vertical>
             <div v-if="currentWorkspace && fileDataStore.hasFileData(currentWorkspace.id)">
               <n-text>数据大小: {{ fileDataStore.getFileData(currentWorkspace.id)?.length || 0 }} 字符</n-text>
               <br>
               <n-text>行数: {{ totalLines }}</n-text>
               <br>
               <n-text>包含dump_info文件夹: {{ hasDumpInfo ? '是' : '否' }}</n-text>
               <br>
               <n-text>前500字符预览:</n-text>
               <n-input
                 type="textarea"
                 :value="preview"
                 :rows="8"
                 readonly
                 placeholder="文件内容将在这里显示..."
               />
             </div>
             <div v-else>
               <n-text>没有解压数据</n-text>
             </div>
           </n-space>
         </n-card>

         <!-- 日志文件分析 -->
         <n-card title="日志文件分析" size="small">
           <n-space vertical>
             <div v-if="currentWorkspace && fileDataStore.hasFileData(currentWorkspace.id) && hasDumpInfo">
               <n-space vertical>
                 <div v-for="(analysis, index) in logFileAnalysis" :key="index" class="analysis-item">
                   <n-card size="small" :title="analysis.title">
                     <n-space vertical>
                       <n-text>{{ analysis.description }}</n-text>
                       <n-text>路径: {{ analysis.path }}</n-text>
                       <n-tag v-if="analysis.found" type="success">找到</n-tag>
                       <n-tag v-else type="warning">未找到</n-tag>
                     </n-space>
                   </n-card>
                 </div>
               </n-space>
             </div>
             <div v-else>
               <n-text>没有dump_info数据或未找到工作区</n-text>
             </div>
           </n-space>
         </n-card>

         <!-- 组件列表 -->
         <n-card title="发现的组件" size="small">
           <n-space vertical>
             <div v-if="currentWorkspace && fileDataStore.hasFileData(currentWorkspace.id) && hasDumpInfo">
               <n-text>组件数量: {{ componentList.length }}</n-text>
               <br>
               <n-space wrap>
                 <n-tag v-for="component in componentList" :key="component" type="info">
                   {{ component }}
                 </n-tag>
               </n-space>
             </div>
             <div v-else>
               <n-text>没有发现组件</n-text>
             </div>
           </n-space>
         </n-card>

        <!-- 所有工作区列表 -->
        <n-card title="所有工作区" size="small">
          <n-space vertical>
            <div v-for="workspace in allWorkspaces" :key="workspace.id" class="workspace-item">
              <n-card size="small" :title="workspace.name">
                <n-space vertical>
                  <n-text>ID: {{ workspace.id }}</n-text>
                  <n-text>文件路径: {{ workspace.filePath || '未设置' }}</n-text>
                  <n-text>解压数据: {{ workspace.extractedData ? `${workspace.extractedData.length} 字符` : '无' }}</n-text>
                  <n-text>创建时间: {{ workspace.createTime }}</n-text>
                </n-space>
              </n-card>
            </div>
            <div v-if="allWorkspaces.length === 0">
              <n-text>没有工作区</n-text>
            </div>
          </n-space>
        </n-card>
      </n-space>
    </n-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { RefreshOutline } from '@vicons/ionicons5'
import { useWorkspaceStore } from '../stores/workspace'
import { useFileDataStore } from '../stores/fileData'
import { 
  LOG_PATHS, 
  LogFileType, 
  extractFileContent, 
  extractComponentList, 
  hasDumpInfoFolder,
  getComponentResourcePath 
} from '../config/logPaths'

const workspaceStore = useWorkspaceStore()
const fileDataStore = useFileDataStore()

// 响应式数据
const currentWorkspace = computed(() => workspaceStore.currentWorkspace)
const allWorkspaces = computed(() => workspaceStore.workspaces)

// 计算属性
const totalLines = computed(() => {
  if (!currentWorkspace.value) return 0
  const fileData = fileDataStore.getFileData(currentWorkspace.value.id)
  if (!fileData) return 0
  return fileData.split('\n').length
})

const preview = computed(() => {
  if (!currentWorkspace.value) return ''
  const fileData = fileDataStore.getFileData(currentWorkspace.value.id)
  if (!fileData) return ''
  return fileData.length > 500 ? fileData.substring(0, 500) + '...' : fileData
})

// 检查是否包含dump_info文件夹
const hasDumpInfo = computed(() => {
  if (!currentWorkspace.value) return false
  const fileData = fileDataStore.getFileData(currentWorkspace.value.id)
  if (!fileData) return false
  return hasDumpInfoFolder(fileData)
})

// 日志文件分析
const logFileAnalysis = computed(() => {
  if (!currentWorkspace.value) return []
  const fileData = fileDataStore.getFileData(currentWorkspace.value.id)
  if (!fileData) return []
  
  const analysis = [
    {
      title: '组件版本信息',
      description: '组件版本日志文件',
      path: LOG_PATHS.VERSION.COMPONENT_VERSION,
      found: fileData.includes(LOG_PATHS.VERSION.COMPONENT_VERSION)
    },
    {
      title: 'BMC版本信息',
      description: 'BMC版本日志文件',
      path: LOG_PATHS.VERSION.BMC_VERSION,
      found: fileData.includes(LOG_PATHS.VERSION.BMC_VERSION)
    },
    {
      title: '操作日志',
      description: '操作日志文件（有转储压缩包）',
      path: LOG_PATHS.LOG_DUMP.OPERATION,
      found: fileData.includes(LOG_PATHS.LOG_DUMP.OPERATION)
    },
    {
      title: '运行日志',
      description: '运行日志文件（有转储压缩包）',
      path: LOG_PATHS.LOG_DUMP.APP,
      found: fileData.includes(LOG_PATHS.LOG_DUMP.APP)
    },
    {
      title: '框架日志',
      description: '框架日志文件（有转储压缩包）',
      path: LOG_PATHS.LOG_DUMP.FRAMEWORK,
      found: fileData.includes(LOG_PATHS.LOG_DUMP.FRAMEWORK)
    },
    {
      title: '告警日志',
      description: '告警日志文件',
      path: LOG_PATHS.APP_DUMP.EVENT,
      found: fileData.includes(LOG_PATHS.APP_DUMP.EVENT)
    }
  ]
  
  return analysis
})

// 组件列表
const componentList = computed(() => {
  if (!currentWorkspace.value) return []
  const fileData = fileDataStore.getFileData(currentWorkspace.value.id)
  if (!fileData) return []
  return extractComponentList(fileData)
})

// 方法
function refreshDebugInfo() {
  console.log('=== 简单调试信息刷新 ===')
  console.log('当前工作区:', currentWorkspace.value)
  console.log('所有工作区:', allWorkspaces.value)
  console.log('localStorage workspaces:', localStorage.getItem('workspaces'))
  console.log('localStorage currentWorkspaceId:', localStorage.getItem('currentWorkspaceId'))
}

onMounted(() => {
  refreshDebugInfo()
})
</script>

<style scoped>
.simple-debug {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.debug-card {
  margin-bottom: 20px;
}

.workspace-item {
  margin-bottom: 10px;
}
</style> 